require([
  'jquery',
  'pageInit',
  'dataTable',
  'loading',
  'uploadFiles'
], function($, vamakerPage, VamDataTable, VamLoader, UploadFiles) {
  $(function() {
    var 
      $dataTable = $('#data-table'),
      $importOpBtn = $('#import-operate-btn'),
      dataUrl = VAM.getVar('TANX_DATA_LIST'),
      dataEidtUrl = VAM.getVar('DATA_EDIT_URL'),
      importUrl = VAM.getVar('DATA_IMPORT_URL'),
      $reportDataThead = $dataTable.find('thead'),
      $loadingWrap = new VamLoader().loader($('#list-loading-wrap')),
    listTable, tableOptions = {};
    // Table settings.
    tableOptions = {
      'ajax': {
        'url': dataUrl
      },
      'order': [5,'desc'],
      'columns': [
        {'data': 'status'},
        {'data': 'name'},
        {'data': 'id'},
        {'data': 'type'},
        {'data': 'content'},
        {'data': 'submitDate'},
        {'data': 'expireDate'},
        {'data': 'effectStatus'}
      ],
      'columnDefs': [
        {
          'targets': [0,1,2,3,4,5,6,7],
          'render': function (data, type, full, meta) {
            return '<p title="' + data + '">' + data +'</p>'
          }
        }
     //    ,
        // {
        //  'targets': -1,
        //  'render': function (data, type, full, meta) {
        //    return '<div class="tab-btn-group">'
        //          + '<a href="' + dataEidtUrl + '?id=' + full.id + '" class="btn btn-primary btn-xs tab-edit" data-type="edit" title="编辑"><i class="fa fa-edit"></i></a>'
        //          + '</div>';
        //  }
        // }
      ]
    };
    $dataTable
    .on('preXhr.dt', function(evt) {
      $loadingWrap.show();
    });
    listTable = new VamDataTable().dataTables($dataTable, tableOptions);
    // After table data list.
    listTable.on('xhr.dt', function(evt) {
      $loadingWrap.fadeOut();
    });

    // Init data import.
    var $uploadMaterial = $('#import-operate-btn'),
      $uploadMaterialModal = $('#upload-material-modal'),
      $uploadMaterialOperate = $('#upload-confirm-operate');

    $uploadMaterialModal.on('hidden.bs.modal', function (evt) {
      var $me = $(this);
      $('#progress .progress-bar').css('width','0%').text('0%');
      $('#files').find('tbody').html('');
    });

    $uploadMaterial.on('click', function (evt) {
      var $me = $(this);
      $uploadMaterialModal.modal('show');
    });
    
    $uploadMaterialOperate.on('click', function(evt) {
      var $me = $(this),
        importStatus = $me.data('importStatus');
      evt.preventDefault();
      // console.log(importStatus);
      $me.data('importStatus', '');
      $uploadMaterialModal.modal('hide');
      if (importStatus !== '') {
        dataList.ajax.reload();
      }
    });
  });
  // Upload Creative Material.
  $(function () {
      'use strict';
      var 
        url = VAM.getVar('DATA_IMPORT_URL'),
        $fileUpload = $('#fileupload'),
        $progressBar = $('#progress .progress-bar'),
        $table = $('#files').find('table tbody'),
        $modalLoadingWrap = $('#modal-loading-wrap');
      
      $fileUpload.fileupload({
          url: url,
          dataType: 'json',
          done: function (e, data) {
            $modalLoadingWrap.hide();
            if (data.result.status === 'success') {
              var materialData = data.result.data,
               list = materialData['table'],
               listStr = '';
              console.log(materialData);
              VAM.fn.Messenger(data.result.msg, 'success');
              $.each(list, function(index, ele) {
                var str = '';
                str += '<tr>'
                      + '<td>' + ele.name + '</td>'
                      + '<td>' + ele.size + '</td>'
                      + '<td>' + ele.date + '</td>'
                      + '<td>' + ele.judgeInfo + '</td>'
                      + '<td>' + ele.updater + '</td>'
                      + '</tr>';
                listStr += str;
              });
              $table.append(listStr);
              $('#upload-confirm-operate').data('importStatus', 'true');
            }else {
              VAM.fn.Messenger(data.result.reason, 'error');
              $('#upload-confirm-operate').data('importStatus', '');
            };
          },
          progressall: function (e, data) {
              var progress = parseInt(data.loaded / data.total * 100, 10);
              $progressBar.css('width','0%').text('0%');
              $progressBar.css(
                  'width',
                  progress + '%'
              ).text(progress + '%');
          }
      })
      .prop('disabled', !$.support.fileInput)
      .parent().addClass($.support.fileInput ? undefined : 'disabled')
      .on('fileuploadstart', function (evt) {
        console.log('before');
        $modalLoadingWrap.show();
      });
  });
});
